package gov.fnal.xpath2ldap;

import gov.fnal.xpath2ldap.util.Configuration;

import java.net.URL;
import java.io.IOException;

import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.CommandLine;


public class XQuerySearchOptions {
   public static final String H = "H";
   public static final String B = "b";
   public static final String USE_LDAP_AUX = "use_ldap_aux";
   public static final String OUTPUT_LDAP_DN = "output_ldap_dn";
   public static final String OUTPUT_ALL_LDAP_ATTRIBUTES = "output_all_ldap_attributes";
   public static final String OUTPUT = "output";
   public static final String OUTPUT_LIST = "list";
   public static final String OUTPUT_XML = "xml";
   public static final String OUTPUT_SOURCE_XML = "source_xml";
   public static final String OUTPUT_DEFAULT = OUTPUT_LIST;
   public static final String ACTION = "action";
   public static final String ACTION_EVALUATE = "evaluate";
   public static final String ACTION_NOT_EVALUATE = "not_evaluate";
   public static final String ACTION_SEARCH = "search";
   public static final String ACTION_DEFAULT = ACTION_EVALUATE;
   public static final String XQUERYFILE = "q";
   public static final String XQUERYFILE_DEFAULT = "_";

    private static Options options;
    public static String version = "put_maven_version_filter_here";
    public static String syntax = "xquerysearch [options] host_url:port xqueryfile";

    public static String header = "Available options:";

    public static String footer = "\n" +
            "\nJava properties:\n" +
            "-Dxpathsearch.home=full path of xpathsearch installation directory\n" +
            "\nTry 'xquerysearch <command> --help' for help on a specific command.\n" +
            "For example 'xquerysearch --help'.\n" +
            "\n" +
            "XQuerySearch -- Search of structured data.\n" +
            "For additional information, see http://cd-97177.fnal.gov:2088/redmine\n" +
            "Bug Reports to <xpathsearch@gmail.com>";

    public static CommandLine processArguments(String[] args) {
        return Configuration.processArguments(args, getOptions(), version, syntax, header, footer);
    }

    public static Options getOptions() {
        if(options==null) setupOptions();
        return options;
    }

    private static void setupOptions() {
        // Read in extra properties from the conf subdirectory of the install directory.
        try {
            URL base_conf_url = Configuration.getModulePropertiesURL("xpathsearch.home");
            URL[] conf_urls = new URL[]{base_conf_url};
            Configuration.processModuleProperties("xpathsearch.properties", conf_urls);
        } catch (
                IOException ioe) {
            System.err.println("Warning: could not process properites file");
        }

        // create the Options
        options = new Options();
        options.addOption("v", "version", false, "");
        options.addOption("h", "help", false, "");
        Option host   = OptionBuilder.hasArg()
                .withArgName("PROTOCOL://HOST:PORT")
                .withDescription(  "host_uri for search. Example: -H ldap://iu.grid.ui.edu:2710" )
                .create( H );
        options.addOption(host);
        Option base   = OptionBuilder.hasArg()
                .withArgName("o=BASE")
                .withDescription(  "base for LDAP search. Example: -b o=grid" )
                .create( B );
        options.addOption(base);
        options.addOption("u", USE_LDAP_AUX, false, "Use auxilary LDAP classes");
        options.addOption("t", "test", false, "Run the unit tests");
        Option output   = OptionBuilder.hasArg()
                .withArgName("OUTPUT")
                .withDescription(  "output format. Optional." +
                        "-output source_xml (Write intermediate xml from datasource)")
                .create( OUTPUT );
        options.addOption(output);
        Option action   = OptionBuilder.hasArg()
                .withArgName("ACTION")
                .withDescription(  "action to perform. Choose from" +
                        "-action evaluate (Evaluate xpath expression. Default.)" +
                        "-action not_evaluate (Do not evaluate xpath expression)")
                .create( ACTION );
        options.addOption(action);
        Option xqueryfile   = OptionBuilder.hasArg()
                .withArgName("XQUERYFILE")
                .withDescription(  "file containing xquery script.")
                .create( XQUERYFILE );
        options.addOption(xqueryfile);
        options.addOption("i", "interactive", false, "Process the expressions interactively");
    }
}